package com.aramco.fion.model.presentation
{
	import com.aramco.fion.view.fion.FionView;
	import com.aramco.fion.view.GroupViewAdmin;
	import com.aramco.fion.view.LeftNavigation;
	import com.aramco.fion.view.SummaryViewAdmin;
	import com.aramco.fion.vo.DashboardVo;
	import com.aramco.fion.vo.FionVo;
	import com.aramco.fion.vo.GroupViewVO;
	import com.aramco.fion.vo.SummaryViewVO;
	
	import flash.events.Event;
	
	import mx.collections.XMLListCollection;
	import mx.containers.Accordion;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Tree;
	import mx.events.ListEvent;

	[Bindable]
	public class LeftNavigationPresentaionModel
	{
		public function LeftNavigationPresentaionModel()
		{
		}
		
		//private var _metaData:XML;
		private var _appMeta:FionVo;
		
		public var view:LeftNavigation;
		
		[Inject("queryModel.appMeta", bind="true")]
		public function get appMeta():FionVo
		{
			return _appMeta;
		}

		public function set appMeta(value:FionVo):void
		{
			_appMeta = value;
			generateNavigation(value);
		}

		
		[ViewAdded]
		public function viewAdded( view : LeftNavigation ) : void
		{
			this.view = view;
		}
		
		public function getArrayForNavigation(value:FionVo):void
		{
			
		}
		
		private function getNavigationXML(value:FionVo):XML
		{
			var xm:XML = <root></root>;
			for(var i:int = 0 ; i < value.dashboards.length;i++)
			{
				var xmlList:XMLList = XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).id+'"'+ ' title="' +(value.dashboards[i] as DashboardVo).title+ '"></node>');
				trace("Dashboard: " + (value.dashboards[i] as DashboardVo).title);
				for(var x:int = 0; x < (value.dashboards[i] as DashboardVo).views.length; x++)
				{
					xmlList.appendChild(new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x]['id'] +'"'+ 
						' title="' + (value.dashboards[i] as DashboardVo).views[x]['title'] +'"'+ '></node>'));
				}
				xm.appendChild(xmlList);
			}
			return xm;
		}
		
		
		private function getNavigationXMLByDashboard(value:DashboardVo):XML
		{
			var xm:XML = <root></root>;
				for(var x:int = 0; x < value.views.length; x++)
				{
					xm.appendChild(new XMLList('<node id=' +'"'+  value.views[x]['id'] +'"'+ 
						' title="' +  value.views[x]['title'] +'"'+
						' type="' +  getViewType(value.views[x]) +'"'+ 
						'></node>'));
				}
			return xm;
		}
		
		
		public function getNavigationXMLByComponents(value:FionVo):XML
		{
			var xm:XML = <root></root>;
			for(var i:int = 0 ; i < value.dashboards.length;i++)
			{
				var dashboardList:XMLList = XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).id+'"'+ ' title="' +(value.dashboards[i] as DashboardVo).title+ '"></node>');
				trace((value.dashboards[i] as DashboardVo).title);
				for(var x:int = 0; x < (value.dashboards[i] as DashboardVo).views.length; x++)
				{
					var viewList:XMLList = new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x]['id'] +'"'+ 
						' title="' + (value.dashboards[i] as DashboardVo).views[x]['title'] +'"'+ '></node>');
					
					dashboardList.appendChild(viewList);
					
					
					trace('-'+(value.dashboards[i] as DashboardVo).views[x]['title']);
					if((value.dashboards[i] as DashboardVo).views[x] is SummaryViewVO)
					{
						//var compList:XMLList = 
						for(var y:int = 0 ; y < (value.dashboards[i] as DashboardVo).views[x].components.length; y++)
						{
							trace('--'+(value.dashboards[i] as DashboardVo).views[x].components[y]['title']);
							viewList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].components[y]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].components[y]['title'] +'"'+ '></node>')));
						}
					}
						
					else if((value.dashboards[i] as DashboardVo).views[x] is GroupViewVO)
					{
						//
						for(var m:int = 0 ; m < (value.dashboards[i] as DashboardVo).views[x].groups.length; m++)
						{
							var groupList:XMLList = XMLList((('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m]['title'] +'"'+ '></node>')));
							for(var n:int = 0; n < (value.dashboards[i] as DashboardVo).views[x].groups[m].components.length; n++)
							{
								trace('--'+(value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title']);
								groupList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['id'] +'"'+ 
									' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title'] +'"'+ '></node>')));
							}
						}
						viewList.appendChild(groupList);
					}
					
				}
				xm.appendChild(dashboardList);
			}
			return xm;
		}
		
		private function getViewType(obj:Object):String
		{
			if(obj is SummaryViewVO)
				return 'SummaryViewVO';
			else if(obj is GroupViewVO)
				return 'GroupViewVO';
			else
				return "";
		}
		
		

		
		
		public function generateNavigation(value:FionVo):void
		{
		//	createXML();
			
			
			var tree:Tree;
			
			//view.removeAllElements();
			var accord:Accordion = new Accordion();
			accord.percentHeight = 100;
			accord.percentWidth = 100;
			
			//Adding Accordions
			for(var i:int = 0 ; i < value.dashboards.length;i++)
			{
				var view:VBox = new VBox();
				view.percentHeight = 100;
				view.percentWidth = 100;
				view.label = (value.dashboards[i] as DashboardVo).title;
				this.view.accord.addElement(view);
				
				//Adding Views
				if(value.dashboards[i].views != null)
				{
					tree = new Tree();
					tree.percentHeight = 100;
					tree.percentWidth = 100;
					tree.dataProvider = getNavigationXMLByDashboard(value.dashboards[i]);
					tree.showRoot = false;
					tree.labelField = "@title";
					view.addElement(tree);
				}
				tree.addEventListener(Event.CHANGE, change);
			}
		}
		
		private function change(event:Event):void
		{
			if(event.currentTarget.selectedItem.@type == "SummaryViewVO")
			{
				this.view.mainArea.removeAllElements();
			//	var view:SummaryViewAdmin = new SummaryViewAdmin();
				var view:FionView = new FionView();
				view.percentHeight = 100;
				view.percentWidth = 100;
				this.view.mainArea.addElement(view);
			}
			
			else if(event.currentTarget.selectedItem.@type == "GroupViewVO")
			{
				this.view.mainArea.removeAllElements();
				var view1:GroupViewAdmin = new GroupViewAdmin();
				view1.percentHeight = 100;
				view1.percentWidth = 100;
				this.view.mainArea.addElement(view1);
			}
		}
	}
}